package com.liangzhong.proxy.mail
{
	import com.adobe.serialization.json.JSON;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.commandconst.pub.CMD_Public;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.mail.MailServerDataParse;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.socket.clientsocket.IClientSocket;
	import com.socket.clientsocket.IClientSocketHandler;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *邮件 
	 * @author lake
	 * 
	 */	
	public class MailProxy extends BaseProxy implements IClientSocketHandler
	{
		public static const ProxyName :String = "MailProxy";
		
		public function MailProxy(proxyName:String=ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Logic.MAIN:
				{
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
				}
				case CMD_Public.MAIN://公用协议
				{
					return onDealPub(mainCmdID,subCmdID,buffer,dataSize);
				}
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 */		
		private function onDealPub(mainCmdID:uint,
								   subCmdID:uint,
								   buffer:ByteArray, 
								   dataSize:int):Boolean
		{
			var retcode :int;
			switch(subCmdID)
			{
				case CMD_Public.SUB_MAIL_RES://收到邮件
				{
					MailServerDataParse.readMailRes(buffer);
					return true;
				}
				case CMD_Public.SUB_MAIL_GET://邮件领取东西
				{
					retcode = buffer.readInt();
					if(retcode == 0)//领取成功
					{
						Alert.showErrorTip("领取成功,物品已到仓库");
						var mailId :int = buffer.readShort();
						AppData.getInstance().mailDataManager.deleteMailById(mailId);
						AppData.getInstance().mailDataManager.unGetNum -- ;
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					return true;
				}
			}
			return false;
		}
		/**
		 *处理信息 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(subCmdID)
			{
				
			}
			return false;
		}
		/**
		 *获取物品 
		 * @param mailId
		 * 
		 */		
		public function getGoods(mailId :int):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,CMD_Public.MAIN,CMD_Public.SUB_MAIL_GET,0);
			sendBuf.writeShort(mailId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *请求邮件信息 
		 * @param $obj
		 * 
		 */
		public function getMailList(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.MAIL_INFO;
			ProxyUtils.instance.webClient(getMailListRes,variables,false);
		}
		/**
		 *返回信息 
		 * @param $data
		 * 
		 */
		private function getMailListRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().mailDataManager.parseMailList(obj.mail);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
	}
}